/**
 * @author ChaP
 * @date 2019/04/21
 * @describe:
 */
package CodingTest.AC20190421;

import java.util.Queue;
import java.util.Stack;

public class decodeString {
    public String decodeString(String s) {
        String res ="";
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ']') {
                String str = "";
                while(!stack.isEmpty() && !stack.peek() .equals("[")){
                    str = stack.pop()+str;
                }
                stack.pop();
                String count1 = "";
                while(!stack.isEmpty() && (stack.peek().charAt(0)<='9' && stack.peek().charAt(0)>='0')){
                    count1 = stack.pop()+count1;
                }
                int count = Integer.parseInt(count1);
                String str1 = "";
                for(int j = 0;j<count;j++){
                    str1= str1+str;
                }
                stack.push(str1);
            }else{
                stack.push(s.charAt(i)+"");
            }
        }
        while(!stack.isEmpty()){
            res=stack.pop()+res;
        }
        return res;
    }

    public static void main(String[] args) {
        decodeString ds = new decodeString();
        String s = "3[a]2[bc]";
        System.out.println(ds.decodeString(s));
    }

}
